package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysEmissionIndexInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysEmissionIndexInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 排放量指标信息表控制器
 * @date 2018/2/27 17:04
 */
@RestController
@RequestMapping("emission/index")
@Api(value = "排放量指标信息表controller",tags = "排放量指标信息表")
public class SysEmissionIndexInfoController {
    
    
    @Autowired
    private SysEmissionIndexInfoService sysEmissionIndexInfoService;


    /**
     * 查询所有排放量指标信息
     * @return
     */
    @ApiOperation(value="获取用户列表", notes="无参数获取所有用户信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<Collection<SysEmissionIndexInfo>> queryUser(){
        return new ResultBean<Collection<SysEmissionIndexInfo>>(sysEmissionIndexInfoService.list());
    }

    /**
     * 新增排放量指标信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增排放量指标信息", notes="新增排放量指标信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "monitorTypeId", value = "监测类型id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "factorId", value = "因子id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "emissionIndexNumber", value = "允许排放量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "indexType", value = "指标类型企业还是辖区", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "add")
    public ResultBean<SysEmissionIndexInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "companyId","areaCode","monitorTypeId","factorId"
                ,"emissionIndexNumber","indexType");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.boolAdd(sysEmissionIndexInfoService.save(payload)) ;
        }
        return  result;
    }

    /**
     * 通过id 查询排放量指标信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id 查询排放量指标信息", notes="根据排放量指标信息的id来获取排放量指标信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "排放量指标信息ID", required = true, dataType = "String", paramType = "query")
    @GetMapping(value = "info")
    public ResultBean<SysEmissionIndexInfo> queryUser( @RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return  result.ok(sysEmissionIndexInfoService.findId(payload.get("id").toString()));
        }
        return  result;
    }

    /**
     * 根据id 删除排放量指标信息
     * @param payload
     */
    @ApiOperation(value="删除排放量指标信息", notes="根据id来指定排放量指标信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "id", value = "排放量指标信息ID", required = true, dataType = "String",paramType = "query")
    @DeleteMapping(value = "/delete")
    public  ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(sysEmissionIndexInfoService.delete(payload.get("id").toString()));
        }
        return result;
    }

    /**
     * 分页查询排放量指标信息
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询排放量指标信息", notes="分页无参数查询排放量指标信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysEmissionIndexInfo>> showUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysEmissionIndexInfo> pageSysEmissionIndexInfo = sysEmissionIndexInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageSysEmissionIndexInfo);
        }
        return result;
    }

    /**
     * 分页查询排放量指标信息
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页带参数查询排放量指标信息", notes="分页带参数查询排放量指标信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging/query")
    public ResultBean<Page<SysEmissionIndexInfo>> PageUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize","areaCode");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            SysEmissionIndexInfo SysEmissionIndexInfo = new SysEmissionIndexInfo();
            SysEmissionIndexInfo.setAreaCode(payload.containsKey("areaCode") ?payload.get("areaCode") != null ? payload.get("areaCode").toString():"":"" );
            Page<SysEmissionIndexInfo> pageSysEmissionIndexInfo = sysEmissionIndexInfoService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),SysEmissionIndexInfo);
            return result.ok(pageSysEmissionIndexInfo);
        }
        return result;
    }
}
